Public Class GDALDataset

    Private obj As Long
    Private owned As Long
    Public XSize As Long
    Public YSize As Long
    Public BandCount As Long

    ' ----------------------------------------------------------------------------
    Public Sub New()
        obj = 0
        owned = 0
        XSize = 0
        YSize = 0
        BandCount = 0
    End Sub


    ' ----------------------------------------------------------------------------
    Public Sub CInit(ByVal obj_in As Long, ByVal owned_in As Long)
        obj = obj_in
        owned = owned_in
        If obj <> 0 Then
            XSize = GDALCore.GDALGetRasterXSize(obj)
            YSize = GDALCore.GDALGetRasterYSize(obj)
            BandCount = GDALCore.GDALGetRasterCount(obj)
        End If
    End Sub
    ' ----------------------------------------------------------------------------
    Public Function IsValid() As Boolean

        If obj = 0 Then
            IsValid = False
        Else
            IsValid = True
        End If
    End Function

    ' ----------------------------------------------------------------------------
    Public Function GetObjPtr() As Long
        GetObjPtr = obj
    End Function

    ' ----------------------------------------------------------------------------
    Public Sub CloseDS()
        If obj <> 0 Then
            'Call MsgBox("CloseDS " & GetDescription() )
            Call GDALCore.GDALClose(obj)
            obj = 0
            owned = 0
        End If
    End Sub

    ' ----------------------------------------------------------------------------
    Public Sub FlushCache()
        If obj <> 0 Then
            Call GDALCore.GDALFlushCache(obj)
        End If
    End Sub

    ' ----------------------------------------------------------------------------
    Public Function GetRasterBand(ByVal BandNo As Long) As GDALRasterBand
        Dim c_obj As Long
        Dim band As GDALRasterBand

        band = Nothing
        If obj <> 0 Then
            c_obj = GDALCore.GDALGetRasterBand(obj, BandNo)
            If c_obj <> 0 Then
                band = New GDALRasterBand
                band.CInit(c_obj)
            End If
        End If
        GetRasterBand = band
    End Function

    ' ----------------------------------------------------------------------------
    Public Function GetProjection() As String
        Dim c_str As Long

        If obj <> 0 Then
            c_str = GDALCore.GDALGetProjectionRef(obj)
            GetProjection = GDALCore.CStr2VB(c_str)
        Else
            GetProjection = ""
        End If
    End Function
    ' ----------------------------------------------------------------------------
    Public Function SetProjection(ByVal WKTProj As String) As String
        Dim err As Long

        If obj <> 0 Then
            SetProjection = GDALCore.GDALSetProjection(obj, WKTProj)
        Else
            SetProjection = GDALCore.ObjIsNULLError
        End If
    End Function
    ' ----------------------------------------------------------------------------
    Public Function GetGeoTransform(ByVal Geotransform() As Double) As Long
        Dim gtsafe(0 To 6) As Double
        ' ReDim gtsafe(6) As Double

        If obj <> 0 Then
            GetGeoTransform = GDALCore.GDALGetGeoTransform(obj, gtsafe(0))
            Geotransform(0) = gtsafe(0)
            Geotransform(1) = gtsafe(1)
            Geotransform(2) = gtsafe(2)
            Geotransform(3) = gtsafe(3)
            Geotransform(4) = gtsafe(4)
            Geotransform(5) = gtsafe(5)
        Else
            GetGeoTransform = GDALCore.ObjIsNULLError
        End If
    End Function
    ' ----------------------------------------------------------------------------
    Public Function SetGeoTransform(ByVal Geotransform() As Double) As Long
        Dim gtsafe(0 To 6) As Double
        'ReDim gtsafe(6) As Double

        If obj <> 0 Then
            Dim i As Integer
            For i = 0 To 5
                gtsafe(i) = Geotransform(i)
            Next i
            SetGeoTransform = GDALCore.GDALSetGeoTransform(obj, gtsafe(0))
        Else
            SetGeoTransform = GDALCore.ObjIsNULLError
        End If
    End Function

    ' ----------------------------------------------------------------------------
    Public Function GetMetadata(ByVal Domain As String) As Object
        If obj <> 0 Then
            GetMetadata = GDALCore.GetMetadata(obj, Domain)
        End If
    End Function

    ' ----------------------------------------------------------------------------
    Public Function SetMetadata(ByVal MetaData As Object, ByVal Domain As String) As Object
        If obj <> 0 Then
            SetMetadata = GDALCore.SetMetadata(obj, MetaData, Domain)
        Else
            SetMetadata = GDALCore.ObjIsNULLError
        End If
    End Function

    ' ----------------------------------------------------------------------------
    Public Function GetMetadataItem(ByVal Name As String, ByVal Domain As String) As String
        If obj <> 0 Then
            GetMetadataItem = GDALCore.CStr2VB(GDALCore.GDALGetMetadataItem(obj, Name, Domain))
        Else
            GetMetadataItem = vbNullString
        End If
    End Function

    ' ----------------------------------------------------------------------------
    Public Function SetMetadataItem(ByVal Name As String, _
    ByVal Value As String, ByVal Domain As String) As Long
        If obj <> 0 Then
            SetMetadataItem = GDALCore.GDALSetMetadataItem(obj, Name, Value, Domain)
        Else
            SetMetadataItem = GDALCore.ObjIsNULLError
        End If
    End Function

    ' ----------------------------------------------------------------------------
    Public Function GetDescription() As String
        If obj <> 0 Then
            GetDescription = GDALCore.CStr2VB(GDALCore.GDALGetDescription(obj))
        End If
    End Function

    Protected Overrides Sub Finalize()
        MyBase.Finalize()
        If obj <> 0 And owned <> 0 Then
            Call CloseDS()
        End If
    End Sub
End Class
